package com.kaiyiweitong.tongxing.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.activity.MainActivity;
import com.kaiyiweitong.tongxing.fragment.coupon_fragment.GetAwardFragment;
import com.kaiyiweitong.tongxing.fragment.coupon_fragment.MoneyCouponFragment;
import com.kaiyiweitong.tongxing.fragment.coupon_fragment.DiscountCouponFragment;
import com.kaiyiweitong.tongxing.other.FragmentFactory;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Administrator on 2017/12/15 0015.
 */

public class CouponFragment extends Fragment implements ViewPager.OnPageChangeListener {
    @Bind(R.id.platform_coupon)
    TextView mPlatformCoupon;
    @Bind(R.id.platform_coupon_line)
    View mPlatformCouponLine;
    @Bind(R.id.get_award)
    TextView mGetAward;
    @Bind(R.id.get_award_line)
    View mGetAwardLine;
    @Bind(R.id.store_coupon)
    TextView mStoreCoupon;
    @Bind(R.id.store_coupon_line)
    View mStoreCouponLine;
    @Bind(R.id.vp_coupon)
    ViewPager mVpCoupon;
    private View mView;

    private List<Fragment> mFragments = new ArrayList<>();
    private MainActivity mActivity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_coupon, null);
        mActivity = (MainActivity) getActivity();
        ButterKnife.bind(this, mView);
        init();
        return mView;
    }

    private void init() {
        mFragments.add(FragmentFactory.getInstance(MoneyCouponFragment.class));
        mFragments.add(FragmentFactory.getInstance(GetAwardFragment.class));
        mFragments.add(FragmentFactory.getInstance(DiscountCouponFragment.class));
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(mActivity.getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }
        };

        mVpCoupon.setAdapter(adapter);
        mVpCoupon.setOffscreenPageLimit(2);
        mVpCoupon.addOnPageChangeListener(this);

        indicator(0);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.platform_coupon, R.id.get_award, R.id.store_coupon})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.platform_coupon:
                indicator(0);
                break;
            case R.id.get_award:
                indicator(1);
                break;
            case R.id.store_coupon:
                indicator(2);
                break;
        }
    }


    private int currentIndex = -1;

    private synchronized void indicator(int position) {
        if (currentIndex != position) {
            mVpCoupon.setCurrentItem(position);
            currentIndex = position;
            if (position == 0) {
                mPlatformCoupon.setSelected(true);
                mGetAward.setSelected(false);
                mStoreCoupon.setSelected(false);

                mPlatformCouponLine.setVisibility(View.VISIBLE);
                mGetAwardLine.setVisibility(View.INVISIBLE);
                mStoreCouponLine.setVisibility(View.INVISIBLE);
            } else if (position == 1) {
                mPlatformCoupon.setSelected(false);
                mGetAward.setSelected(true);
                mStoreCoupon.setSelected(false);

                mPlatformCouponLine.setVisibility(View.INVISIBLE);
                mGetAwardLine.setVisibility(View.VISIBLE);
                mStoreCouponLine.setVisibility(View.INVISIBLE);
            } else {
                mPlatformCoupon.setSelected(false);
                mGetAward.setSelected(false);
                mStoreCoupon.setSelected(true);

                mPlatformCouponLine.setVisibility(View.INVISIBLE);
                mGetAwardLine.setVisibility(View.INVISIBLE);
                mStoreCouponLine.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onPageSelected(int position) {
        indicator(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

}
